using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FallenValkyrie
{
    class EnemyType1 : Character
    {
        Animation Idle, Run, AtkRdy, Atk, JumpUp, JumpDown, GetHit;

        public int HP { get; set; }
        public Rectangle enemyRect { get; set; }
 

        public bool Load(ContentManager content)
        {
            Idle = new Animation(content, "Characters/Enemy/1/idle", 128, 144, 8, 15);
            Run = new Animation(content, "Characters/Enemy/1/run", 144, 128, 8, 30);
            AtkRdy = new Animation(content, "Characters/Enemy/1/atk", 150, 136, 2, 6);
            Atk = new Animation(content, "Characters/Enemy/1/atk", 156, 136, 6, 6);
            JumpUp = new Animation(content, "Characters/Enemy/1/jump", 116, 144, 4, 3);
            //JumpDown = new Animation(content, "Enemy/1/jump", 116, 144, 8, 3);
            JumpDown = new Animation(content, "Characters/Enemy/1/jump", 120, 138, 8, 3);
            GetHit = new Animation(content, "Characters/Enemy/1/hit2", 128, 128, 1, 1);


            Idle.EnableRepeating();
            Run.EnableRepeating();
            AtkRdy.EnableRepeating();
            Atk.EnableRepeating();
            Atk.SetStartingFrame(3);
            JumpUp.EnableRepeating();
            //JumpDown.SetStartingFrame(5);
            //JumpDown.SetEndingFrame(5);
            JumpDown.EnableRepeating();
            JumpDown.SetStartingFrame(6);

            return true;
        }

        #region Update Animation
        public void Update(GameTime gameTime)
        {
            UpdateControl(gameTime);
            SightChecking();
        }
        #endregion
        
        private void SightChecking()
        {
            if (spriteEffects == SpriteEffects.None)
                SightDirection = "left";
            else
                SightDirection = "right";
        }

        #region Update Control

        KeyboardState mPreviousKeyboardState;
        public SpriteEffects spriteEffects = SpriteEffects.FlipHorizontally;

        public Vector2 feetPosition = new Vector2(0, 618);
        //Vector2 mSpeed = Vector2.Zero;
        //Vector2 mDirection = Vector2.Zero;
        //Vector2 mStartingPosition = Vector2.Zero;

        int CHARACTER_SPEED = 50;
        int MOVE_LEFT = -5;
        int MOVE_RIGHT = 5;
        int MOVE_UP = -5;
        int MOVE_DOWN = 5;

        enum State
        {
            Idle,
            Run_to_Left,
            Run_to_Right,
            Jump_Up,
            Jump_Down,
            Attack,
            Ready_Heavy_Attacking,
            Heavy_Attacking,
            Get_Hit
            //Jump_Attacking,
        }
        public float getHitTimeDelay = 0;
        public bool getHitState;

        State mCurrentState = State.Idle;
        public string State_AI;
        public bool StateEnd = true;

        bool hasJumped;

        private void UpdateControl(GameTime gameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();
            feetPosition += mDirection * mSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
        
            //UpdateAIState();
            UpdateMovement(aCurrentKeyboardState, gameTime);
            UpdateAttack(gameTime);
            UpdateJump(aCurrentKeyboardState, gameTime);
            UpdateGetHit(gameTime);

            mPreviousKeyboardState = aCurrentKeyboardState;
        }

        private void UpdateMovement(KeyboardState aCurrentKeyboardState, GameTime gameTime)
        {
            Console.WriteLine("mCurrentState: " + mCurrentState + "  State_AI: " + State_AI + "  StateEnd: " + StateEnd);
            if (mCurrentState == State.Idle)
            {
                mSpeed = Vector2.Zero;
                mDirection = Vector2.Zero;

                Idle.Update(gameTime);
                
                //if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && !aCurrentKeyboardState.IsKeyDown(Keys.Right))
                if (State_AI == "run to left")
                    mCurrentState = State.Run_to_Left;

                if (State_AI == "run to right")
                    mCurrentState = State.Run_to_Right;
            }

            if (mCurrentState == State.Run_to_Left)
            {
                Run.Update(gameTime);

                spriteEffects = SpriteEffects.None;
                mSpeed.X = CHARACTER_SPEED;
                mDirection.X = MOVE_LEFT;

                if (State_AI == "idle")
                   mCurrentState = State.Idle;
            }


            if (mCurrentState == State.Run_to_Right)
            {
                Run.Update(gameTime);

                spriteEffects = SpriteEffects.FlipHorizontally;
                mSpeed.X = CHARACTER_SPEED;
                mDirection.X = MOVE_RIGHT;

                if (State_AI == "idle")
                    mCurrentState = State.Idle;
            }

            //if (aCurrentKeyboardState.IsKeyDown(Keys.Z) && mPreviousKeyboardState.IsKeyUp(Keys.Z))
            if (State_AI == "attack")
                mCurrentState = State.Attack;

            //if (aCurrentKeyboardState.IsKeyDown(Keys.X) && mPreviousKeyboardState.IsKeyUp(Keys.X))
               // mCurrentState = State.Heavy_Attacking;

            if (getHitState)
            {
                State_AI = "get hit";
                mCurrentState = State.Get_Hit;
            }

            // if(mCurrentState = State.Get_Hit && timerget hit>=2sec)
            

        }

        private void UpdateAttack(GameTime gameTime)
        {
            if (mCurrentState == State.Attack)
            {
                Atk.Update(gameTime);
                DmgAnimation = true;
                //if (AtkSEIns.State != SoundState.Playing)
                    //AtkSEIns.Play();


                if (spriteEffects == SpriteEffects.FlipHorizontally)
                {
                    mSpeed.X = CHARACTER_SPEED;
                    mDirection.X = MOVE_RIGHT;
                    AtkDirection = "right";
                }
                else
                {
                    mSpeed.X = CHARACTER_SPEED;
                    mDirection.X = MOVE_LEFT;
                    AtkDirection = "left";
                }

                timeDelay += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (timeDelay > 0.25)
                {
                    //mCurrentState = State.Idle;
                    if (hasJumped == true)
                    {
                        mCurrentState = State.Jump_Down;
                    }
                    else
                    {
                        mCurrentState = State.Idle;
                        State_AI = "idle";
                    }

                    timeDelay = 0;
                    DmgAnimation = false;
                }
            }

            /*
            if (mCurrentState == State.Ready_Heavy_Attacking)
            {
                //HeavyAtkRdy.Update(gameTime);

                timeDelay += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (timeDelay > 0.5)
                {
                    mCurrentState = State.Heavy_Attacking;
                    timeDelay = 0;
                }

            }*/
            /*
            if (mCurrentState == State.Heavy_Attacking)
            {
                //HeavyAtk.Update(gameTime);
                AtkAnimation = true;

                if (spriteEffects == SpriteEffects.FlipHorizontally)
                {
                    mSpeed.X = CHARACTER_SPEED;
                    mDirection.X = MOVE_RIGHT;
                    AtkDirection = "right";
                }
                else
                {
                    mSpeed.X = CHARACTER_SPEED;
                    mDirection.X = MOVE_LEFT;
                    AtkDirection = "left";
                }

                timeDelay += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (timeDelay > 0.25)
                {
                    mCurrentState = State.Idle;
                    timeDelay = 0;
                    AtkAnimation = false;
                }
            }*/

        }

        public void UpdateGetHit(GameTime gameTime)
        {
            //if (State_AI == "get hit")
                //mCurrentState = State.Get_Hit;

            if (mCurrentState == State.Get_Hit)
            {
                GetHit.Update(gameTime);

                timeDelay += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (spriteEffects == SpriteEffects.FlipHorizontally)
                {
                    mSpeed.X = 150;
                    mDirection.X = 1;
                }
                else
                {
                    mSpeed.X = 150;
                    mDirection.X = -1;
                }

                if (timeDelay > 0.5f)
                {
                    //Console.WriteLine(getHitState);
                    //Console.WriteLine(mCurrentState);
                    //mCurrentState = State.Run_to_Left;

                    if (hasJumped == true)
                        mCurrentState = State.Jump_Down;
                    else
                    {
                        mCurrentState = State.Idle;
                        State_AI = "idle";
                    }

                    getHitState = false;
                    
                    //doneGetHit = true;
                    //Console.WriteLine("EnemyType1: " + getHitState);
                    timeDelay = 0;
                }
            }
        }
        
        private void UpdateJump(KeyboardState aCurrentKeyboardState, GameTime gameTime)
        {
            if (mCurrentState == State.Idle || mCurrentState == State.Run_to_Left || mCurrentState == State.Run_to_Right)
            {
                if (State_AI == "jump")
                {
                    Jump();
                    hasJumped = true;
                }
            }

            if (mCurrentState == State.Jump_Up)
            {
                JumpUp.Update(gameTime);

                //Console.WriteLine(feetPosition.Y);

                if (mStartingPosition.Y - feetPosition.Y > 300)
                    mCurrentState = State.Jump_Down;
            }

            if (mCurrentState == State.Jump_Down)
            {
                StateEnd = false;

                mDirection.Y = MOVE_DOWN;
                
                if (feetPosition.Y > mStartingPosition.Y)
                {
                    feetPosition.Y = mStartingPosition.Y;

                    JumpDown.Update(gameTime);

                    timeDelay += (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (timeDelay > 0.15)
                    {
                        mCurrentState = State.Idle;
                        timeDelay = 0;
                        mDirection = Vector2.Zero;
                        State_AI = "idle";
                        StateEnd = true;
                    }
                }
            }
        }

        private void Jump()
        {
            if (mCurrentState != State.Jump_Up)
            {
                mCurrentState = State.Jump_Up;
                mStartingPosition = feetPosition;
                mDirection.Y = MOVE_UP;
                mSpeed = new Vector2(50, CHARACTER_SPEED);
            }
        }
        #endregion

        public void Draw(SpriteBatch spriteBatch)
        {
            if (mCurrentState == State.Idle)
                Idle.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);
            else if (mCurrentState == State.Run_to_Left || mCurrentState == State.Run_to_Right)
                Run.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);

            if (mCurrentState == State.Attack)
                Atk.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);
            //else if (mCurrentState == State.Ready_Heavy_Attacking)
                //HeavyAtkRdy.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);
            //else if (mCurrentState == State.Heavy_Attacking)
                //HeavyAtk.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);

            if (mCurrentState == State.Jump_Up)
                JumpUp.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);
            else if (mCurrentState == State.Jump_Down)
                JumpDown.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);

            if (mCurrentState == State.Get_Hit)
                GetHit.Draw(spriteBatch, feetPosition, spriteEffects, (float)MathHelper.ToRadians(0), 1.0f);
        }
        /*
        public void intersect(Rectangle source, Rectangle target) {
            
        }*/
            
    }
}
